/**
 * 警告弹窗;
 */
import uiPanel from "./../../lib/UiPanel";
const { ccclass, property } = cc._decorator;
@ccclass
export default class Alert extends uiPanel {
	@property(cc.Label)
	content: cc.Label = null; //显示的文本内容

	@property(cc.Node)
	btnOK: cc.Node = null; //确定按钮

	@property(cc.Node)
	btnCancel: cc.Node = null; //取消按钮
	@property(cc.Sprite)
	title: cc.Sprite = null; //弹框标题
	@property({
		type: [cc.SpriteFrame],
		displayName: "titleArray"
	})
	titleArray: cc.SpriteFrame[] = [];

	_ok_cb = null;
	_cancel_cb = null;

	onLoad() { }

	init(args) {
		var content = "";
		var onOk = function () { };
		var onCancel = null;
		var titleIndex = -1;

		switch (args.length) {
			case 1:
				content = args[0];
				break;
			case 2:
				content = args[0];
				onOk = args[1];
				break;
			case 3:
				content = args[0];
				onOk = args[1];
				onCancel = args[2];
				break;
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
				content = args[0];
				onOk = args[1];
				onCancel = args[2];
				titleIndex = args[3];
				break;
		}
		this.content.string = content;
		this.btnOK.active = false;
		this.btnCancel.active = false;
		if (onCancel) {
			this._cancel_cb = onCancel;
			this.btnCancel.active = true;
		}
		if (onOk) {
			this._ok_cb = onOk;
			this.btnOK.active = true;
		}
		if (titleIndex >= 0 && titleIndex < this.titleArray.length) {
			if (this.title) {
				this.title.spriteFrame = this.titleArray[titleIndex];
			}
		}

	}

	/**
	 * show alert;
	 * @param {*显示内容} content
	 * @param {*确定的回调函数} onok
	 * @param {*取消的回调事件} oncancel
	 */
	show(content, onOk = function () { }, onCancel, isNoBack) {
		this.node.active = true;

		this.content.string = content;
		this.btnOK.active = false;
		this.btnCancel.active = false;
		if (onCancel) {
			this._cancel_cb = onCancel;
			this.btnCancel.active = true;
		}
		if (onOk) {
			this._ok_cb = onOk;
			this.btnOK.active = true;
		}
	}

	onClickOk() {
		this._ok_cb();
		console.log("alert_ok");
		this.close();
	}
	onClickCancle() {
		this._cancel_cb && this._cancel_cb();
		console.log("alert_cancle");
		this.close();
	}

	onClickBack() {
		this.close();
	}
	close() {
		cc.vv.UIMgr.close(cc.vv.GLConfig.alert_path, cc.vv.UIMgr.closeType.destory);
	}
}
